package com.ydm.fire_fighting_web.modules.management.managementInstitutions.web;

import com.ydm.fire_fighting_web.entity.management.MIListRequest;
import com.ydm.fire_fighting_web.entity.management.ManagementInstitutions;
import com.ydm.fire_fighting_web.exception.response.QueryResult;
import com.ydm.fire_fighting_web.exception.response.ResponseResult;
import com.ydm.fire_fighting_web.modules.management.managementInstitutions.api.ManagementInstitutionsControllerApi;
import com.ydm.fire_fighting_web.modules.management.managementInstitutions.service.ManagementInstitutionsService;
import com.ydm.fire_fighting_web.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebResult;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/manains")
public class ManagementInstitutionsController implements ManagementInstitutionsControllerApi{
    @Autowired
    private ManagementInstitutionsService managementInstitutionsService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private HttpServletResponse response;
    @Override
    @GetMapping("/findByPage/{page}/{size}")
    public QueryResult<ManagementInstitutions> findListByPage(@PathVariable("page")Integer page, @PathVariable("size") Integer size, MIListRequest miListRequest) {
        return managementInstitutionsService.findListByPage(page,size,miListRequest);
    }

    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ManagementInstitutions managementInstitutions) {
        return managementInstitutionsService.save(managementInstitutions);
    }

    @Override
    @GetMapping("/findById/{id}")
    public ManagementInstitutions findById(@PathVariable("id") String id) {
        return managementInstitutionsService.findById(id);
    }

    @Override
    @PutMapping("/update/{id}")
    public ResponseResult update(@PathVariable("id") String id,@RequestBody ManagementInstitutions managementInstitutions) {
        return managementInstitutionsService.update(id,managementInstitutions);
    }

    @Override
    @GetMapping("/export")
    @ResponseBody
    public void export() {
        QueryResult<ManagementInstitutions> all = managementInstitutionsService.findAll();
        List<ManagementInstitutions> institutionsList = all.getList();
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet("获取excel测试表格");

        HSSFRow row = null;

        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue("用户信息列表");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(rowRegion);

		/*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
		sheet.addMergedRegion(columnRegion);*/


        /*
         * 动态获取数据库列 sql语句 select COLUMN_NAME from INFORMATION_SCHEMA.Columns where table_name='user' and table_schema='test'
         * 第一个table_name 表名字
         * 第二个table_name 数据库名称
         * */
        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("Id");//为第一个单元格设值
        row.createCell(1).setCellValue("管理机构名称");//为第二个单元格设值
        row.createCell(2).setCellValue("地址");//为第三个单元格设值
        row.createCell(3).setCellValue("联系人");//为第三个单元格设值
        row.createCell(4).setCellValue("联系电话");//为第三个单元格设值
        for (int i = 0; i < institutionsList.size(); i++) {
            row = sheet.createRow(i + 4);
            ManagementInstitutions obj = institutionsList.get(i);
            row.createCell(0).setCellValue(obj.getId());
            row.createCell(1).setCellValue(obj.getMiName());
            row.createCell(2).setCellValue(obj.getAddress());
            row.createCell(3).setCellValue(obj.getPhoneName());
            row.createCell(4).setCellValue(obj.getPhoneTel());
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));
        //列宽自适应
        for (int i = 0; i <= 13; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            response.setHeader("Content-disposition", "attachment;filename=ManagementInstitutions.xls");//默认Excel名称
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/import")
    public String exImport(@PathVariable("filename")MultipartFile file, HttpSession session) {
        String fileName = file.getOriginalFilename();
        try {
            ResponseResult responseResult = managementInstitutionsService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:findByPage/{page}/{size}";
    }
}
